<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - CPPSearch</title>
    <link rel="stylesheet" href="css/styles.css">
    <style>
        .register-container {
            max-width: 450px;
            margin: 50px auto;
            padding: 40px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .register-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .form-group label {
            font-weight: bold;
            color: #333;
        }
        .form-group input {
            padding: 12px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        .form-group input:focus {
            outline: none;
            border-color: #007bff;
        }
        .form-group input.error {
            border-color: #dc3545;
        }
        .password-strength {
            height: 5px;
            background: #ddd;
            border-radius: 3px;
            margin-top: 5px;
            overflow: hidden;
        }
        .password-strength-bar {
            height: 100%;
            transition: width 0.3s, background-color 0.3s;
            width: 0%;
        }
        .password-strength-bar.weak {
            background: #dc3545;
            width: 33%;
        }
        .password-strength-bar.medium {
            background: #ffc107;
            width: 66%;
        }
        .password-strength-bar.strong {
            background: #28a745;
            width: 100%;
        }
        .register-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 15px;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .register-btn:hover {
            background: #218838;
        }
        .register-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
        .login-link a {
            color: #007bff;
            text-decoration: none;
        }
        .login-link a:hover {
            text-decoration: underline;
        }
        .error-message {
            color: #dc3545;
            text-align: center;
            margin-top: 10px;
            display: none;
        }
        .success-message {
            color: #28a745;
            text-align: center;
            margin-top: 10px;
            display: none;
        }
        .validation-message {
            color: #6c757d;
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>CPPSearch</h1>
            <nav>
                <ul>
                    <li><a href="index.html">首页</a></li>
                    <li><a href="login.html">登录</a></li>
                    <li><a href="register.html" class="active">注册</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <main>
        <div class="container">
            <div class="register-container">
                <h2 style="text-align: center; margin-bottom: 30px;">用户注册</h2>
                <form id="registerForm" class="register-form">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required placeholder="请输入用户名（3-20个字符）">
                        <div class="validation-message" id="usernameValidation"></div>
                    </div>
                    <div class="form-group">
                        <label for="email">邮箱</label>
                        <input type="email" id="email" name="email" required placeholder="请输入邮箱地址">
                        <div class="validation-message" id="emailValidation"></div>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" required placeholder="请输入密码（至少6个字符）">
                        <div class="password-strength">
                            <div class="password-strength-bar" id="passwordStrengthBar"></div>
                        </div>
                        <div class="validation-message" id="passwordValidation"></div>
                    </div>
                    <div class="form-group">
                        <label for="confirmPassword">确认密码</label>
                        <input type="password" id="confirmPassword" name="confirmPassword" required placeholder="请再次输入密码">
                        <div class="validation-message" id="confirmPasswordValidation"></div>
                    </div>
                    <button type="submit" class="register-btn" id="registerBtn">注册</button>
                </form>
                <div class="error-message" id="errorMessage"></div>
                <div class="success-message" id="successMessage"></div>
                <div class="login-link">
                    <p>已有账号？<a href="login.html">立即登录</a></p>
                </div>
            </div>
        </div>
    </main>

    <footer>
        <div class="container">
            <p>&copy; 2025 CPPSearch. 保留所有权利。</p>
        </div>
    </footer>

    <script>
        // 用户名验证
        document.getElementById('username').addEventListener('input', function() {
            const username = this.value;
            const validation = document.getElementById('usernameValidation');
            
            if (username.length < 3) {
                validation.textContent = '用户名至少需要3个字符';
                validation.style.color = '#dc3545';
                this.classList.add('error');
            } else if (username.length > 20) {
                validation.textContent = '用户名不能超过20个字符';
                validation.style.color = '#dc3545';
                this.classList.add('error');
            } else if (!/^[a-zA-Z0-9_]+$/.test(username)) {
                validation.textContent = '用户名只能包含字母、数字和下划线';
                validation.style.color = '#dc3545';
                this.classList.add('error');
            } else {
                validation.textContent = '用户名格式正确';
                validation.style.color = '#28a745';
                this.classList.remove('error');
            }
        });
        
        // 邮箱验证
        document.getElementById('email').addEventListener('input', function() {
            const email = this.value;
            const validation = document.getElementById('emailValidation');
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            
            if (!emailRegex.test(email)) {
                validation.textContent = '请输入有效的邮箱地址';
                validation.style.color = '#dc3545';
                this.classList.add('error');
            } else {
                validation.textContent = '邮箱格式正确';
                validation.style.color = '#28a745';
                this.classList.remove('error');
            }
        });
        
        // 密码强度检测
        document.getElementById('password').addEventListener('input', function() {
            const password = this.value;
            const strengthBar = document.getElementById('passwordStrengthBar');
            const validation = document.getElementById('passwordValidation');
            
            let strength = 0;
            if (password.length >= 6) strength++;
            if (password.length >= 10) strength++;
            if (/[a-z]/.test(password) && /[A-Z]/.test(password)) strength++;
            if (/\d/.test(password)) strength++;
            if (/[^a-zA-Z\d]/.test(password)) strength++;
            
            strengthBar.className = 'password-strength-bar';
            if (password.length > 0) {
                if (strength <= 2) {
                    strengthBar.classList.add('weak');
                    validation.textContent = '密码强度：弱';
                    validation.style.color = '#dc3545';
                } else if (strength <= 3) {
                    strengthBar.classList.add('medium');
                    validation.textContent = '密码强度：中';
                    validation.style.color = '#ffc107';
                } else {
                    strengthBar.classList.add('strong');
                    validation.textContent = '密码强度：强';
                    validation.style.color = '#28a745';
                }
            } else {
                validation.textContent = '';
            }
        });
        
        // 确认密码验证
        document.getElementById('confirmPassword').addEventListener('input', function() {
            const password = document.getElementById('password').value;
            const confirmPassword = this.value;
            const validation = document.getElementById('confirmPasswordValidation');
            
            if (confirmPassword !== password) {
                validation.textContent = '两次输入的密码不一致';
                validation.style.color = '#dc3545';
                this.classList.add('error');
            } else {
                validation.textContent = '密码匹配';
                validation.style.color = '#28a745';
                this.classList.remove('error');
            }
        });
        
        // 表单提交
        document.getElementById('registerForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const email = document.getElementById('email').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const registerBtn = document.getElementById('registerBtn');
            const errorMessage = document.getElementById('errorMessage');
            const successMessage = document.getElementById('successMessage');
            
            // 重置消息
            errorMessage.style.display = 'none';
            successMessage.style.display = 'none';
            
            // 验证输入
            if (username.length < 3 || username.length > 20 || !/^[a-zA-Z0-9_]+$/.test(username)) {
                errorMessage.textContent = '请输入有效的用户名';
                errorMessage.style.display = 'block';
                return;
            }
            
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                errorMessage.textContent = '请输入有效的邮箱地址';
                errorMessage.style.display = 'block';
                return;
            }
            
            if (password.length < 6) {
                errorMessage.textContent = '密码至少需要6个字符';
                errorMessage.style.display = 'block';
                return;
            }
            
            if (password !== confirmPassword) {
                errorMessage.textContent = '两次输入的密码不一致';
                errorMessage.style.display = 'block';
                return;
            }
            
            // 禁用注册按钮
            registerBtn.disabled = true;
            registerBtn.textContent = '注册中...';
            
            try {
                const response = await fetch('/api/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        email: email,
                        password: password
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    successMessage.textContent = '注册成功！正在跳转到登录页面...';
                    successMessage.style.display = 'block';
                    
                    // 延迟跳转到登录页面
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                } else {
                    errorMessage.textContent = result.message || '注册失败，请稍后重试';
                    errorMessage.style.display = 'block';
                }
            } catch (error) {
                errorMessage.textContent = '网络错误，请稍后重试';
                errorMessage.style.display = 'block';
                console.error('Register error:', error);
            } finally {
                // 恢复注册按钮
                registerBtn.disabled = false;
                registerBtn.textContent = '注册';
            }
        });
    </script>
</body>
</html>